<?php
/**
 * 技师实时数据服务层
 */
namespace app\service;
use think\Db;
class TechnLastService
{
	/**
	 * 技师等级升级操作
	 */
	public static function TechnLevelUpgradeHandle($techn_id)
	{
		if(!empty($techn_id))
		{
			//查询技师实时数据
			$techn_last_info = Db::name('techn_last')->where(['techn_id' => $techn_id])->find();
			if(!empty($techn_last_info))
			{
			    
			    if(empty($techn_last_info['divide_into'])){
			    
    			    $city = Db::name('techn')->where(['id' => $techn_id])->value('city');
    			    
    			    $proxy=db::name('proxy')->where('shi',$city)->find();
    			    
    			    
    				//如果技师实时数据存在
    				//检查升级条件
    				$techn_level_map = [
    					'is_delete_time' => 0,
    					'low_result' => ['elt', $techn_last_info['total_result']],
    				 	'online_duration' => ['elt', $techn_last_info['total_duration']],
    				 	'plus_clock_price' => ['elt', $techn_last_info['total_plus_clock_price']],
    				 	'integral' => ['elt', $techn_last_info['total_integral']],
    					'proxy_id'=>$proxy['proxy_id']
    				];
    				$techn_level_info = Db::name('techn_level')->where($techn_level_map)->order('rank desc')->find();
    				
    				if(empty($techn_level_info)){
    				    $techn_level_map = [
        					'is_delete_time' => 0,
        					'low_result' => ['elt', $techn_last_info['total_result']],
        				 	'online_duration' => ['elt', $techn_last_info['total_duration']],
        				 	'plus_clock_price' => ['elt', $techn_last_info['total_plus_clock_price']],
        				 	'integral' => ['elt', $techn_last_info['total_integral']],
        					'proxy_id'=>0
        				];
        				$techn_level_info = Db::name('techn_level')->where($techn_level_map)->order('rank desc')->find();
    				}
    				//dump($techn_level_info);
    				if(!empty($techn_level_info))
    				{
    					$data = [
    						'nature_rank' => $techn_level_info['rank'] > $techn_last_info['nature_rank'] ? $techn_level_info['rank'] : $techn_last_info['nature_rank'],
    						'divide_rank' => $techn_level_info['rank'] > $techn_last_info['divide_rank'] ? $techn_level_info['rank'] : $techn_last_info['divide_rank'],
    						'upd_time' => time(),
    						'divide_into'=>$techn_level_info['divide_into'],
    						'jz_bl'=>$techn_level_info['jz_bl'],
    						'level_id'=>$techn_level_info['id']
    					];
    					//更新用户等级
    					Db::name('techn_last')->where(['techn_id' => $techn_id])->update($data);
    				}
				
			    }
			}
		}
	}
}